from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField,ValidationError
from wtforms.validators import DataRequired,Length,EqualTo
from app.models import User

class Loginform(FlaskForm):
	username=StringField('用户名',validators=[Length(1,20)])
	password=PasswordField('密码',validators=[DataRequired()])
	remember_me=BooleanField('保持登录')
	submit=SubmitField('登录')
	
	
class RegistrationForm(FlaskForm):
	username=StringField('用户名',validators=[Length(1,20)])
	password = PasswordField('密码', validators=[DataRequired(),EqualTo('password2',message='两次输入密码必须相同')])
	password2 = PasswordField('确认密码', validators=[DataRequired()])
	submit=SubmitField('注册')
	
def validate_username(self, field):
	if User.query.filter_by(username=field.data).first():
		raise ValidationError('Username already in use.')
	

class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('旧密码', validators=[DataRequired()])
    password = PasswordField('新密码', validators=[DataRequired(), EqualTo('password2', message='两次密码必须一致')])
    password2 = PasswordField('确认密码',validators=[DataRequired()])
    submit = SubmitField('确认修改')
